package interfaceGrafica;

import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.text.DecimalFormat;
import java.text.DecimalFormatSymbols;

import javax.swing.*;
import javax.swing.table.DefaultTableModel;
import javax.swing.table.TableRowSorter;

import entidades.Funcionario;

import servicos.Servicos;

public class ListarFuncionario extends SuperJanela {

	private JTable jTable;
	private DefaultTableModel modelo;
	private JLabel lblTitulo, lblProcurar;
	private JTextField txtBusca;
	private JButton btnModificar, btnBuscar;
	private String[] cabecalho = { "teste", "teste" };
	private String[] teste = null;
	private JScrollPane scroll;
	private JPanel centro;
	private JPanel norte;
	private JPanel sul;
	private JPanel leste;
	private String[][] dados;
	private GridBagConstraints c;
	private Funcionario[] func;
	private DecimalFormat paraVirgula;
	public ListarFuncionario(Funcionario[] funcionarios) {

		paraVirgula = new DecimalFormat("0.00");
		paraVirgula.setMaximumFractionDigits(2);
		DecimalFormatSymbols decimalVirgula = new DecimalFormatSymbols();
		decimalVirgula.setDecimalSeparator(',');
		paraVirgula.setDecimalFormatSymbols(decimalVirgula);
		
		
		this.func = funcionarios;
		this.setSize(600, 525);
		this.setResizable(true);
		centro = new JPanel(new FlowLayout());
		leste = new JPanel(new FlowLayout());
		sul = new JPanel(new GridBagLayout());
		this.setLayout(new BorderLayout());
		this.setLocationRelativeTo(getOwner());
		try {

			dados = new String[funcionarios.length][4];
			for (int i = 0; i < funcionarios.length; i++) {
				dados[i][0] = funcionarios[i].getCpf().toUpperCase();
				dados[i][1] = funcionarios[i].getNome().toUpperCase();
				dados[i][2] = funcionarios[i].getCargo().getDescricacao()
						.toUpperCase();
				dados[i][3] = paraVirgula.format(funcionarios[i].getSalario());

			}

			this.setTitle("Lista de Funcionarios");
			modelo = new DefaultTableModel(dados, new String[] { "CPF", "Nome",
					"Cargo", "Salario" }) {

				public boolean isCellEditable(int Row, int Collumn) {
					return false;
				}
			};

			jTable = new JTable(modelo);
			jTable.getColumnModel().getColumn(0).setPreferredWidth(90);
			jTable.getColumnModel().getColumn(0).setResizable(false);
			jTable.getColumnModel().getColumn(1).setPreferredWidth(200);
			jTable.getColumnModel().getColumn(1).setResizable(false);
			jTable.getColumnModel().getColumn(2).setPreferredWidth(85);
			jTable.getColumnModel().getColumn(2).setResizable(false);
			jTable.getTableHeader().setReorderingAllowed(false);
			jTable.setSelectionMode(0);
			jTable.setAutoResizeMode(JTable.AUTO_RESIZE_OFF);
			jTable.addMouseListener(new duploClick());
			lblTitulo = new JLabel("Tabela de funcionarios");
			lblTitulo.setFont(new Font("Verdana", Font.BOLD, 15));

			lblProcurar = new JLabel("Procurar");
			lblProcurar.setFont(new Font("Verdana", Font.BOLD, 15));

			c = new GridBagConstraints();
			norte = new JPanel(new FlowLayout());
			norte.add(lblTitulo);
			norte.setAlignmentX(CENTER_ALIGNMENT);
			this.add(norte, BorderLayout.NORTH);

			scroll = new JScrollPane(jTable);
			scroll.setAutoscrolls(true);

			centro.add(scroll);

			c.anchor = c.WEST;
			c.insets = new Insets(5, 5, 5, 5);
			c.gridx = 1;
			c.gridy = 1;

			sul.add(lblProcurar, c);

			c.gridx = 2;
			c.gridy = 1;

			txtBusca = new JTextField(20);
			sul.add(txtBusca, c);
			this.add(sul, BorderLayout.SOUTH);

			txtBusca.addKeyListener(new Listar());

			btnModificar = new JButton("Modificar");
			btnModificar.addActionListener(new Modificar());
			leste.add(btnModificar);
			this.add(leste, BorderLayout.EAST);
			leste.setAlignmentX(CENTER_ALIGNMENT);

			this.add(centro, BorderLayout.CENTER);

			setVisible(true);

		} catch (Exception ex) {
			JOptionPane.showMessageDialog(this, "Erro Interno:\r\n " + ex, "Erro Interno", JOptionPane.ERROR_MESSAGE);
		}
	}

	public static void main(String args[]) {

	}

	private class Modificar implements ActionListener {

		public void actionPerformed(ActionEvent e) {
			boolean flag = true;
			int cont = 0;

			while (flag) {
				if (jTable.getValueAt(jTable.getSelectedRow(), 0) == dados[cont][0]) {
					flag = false;
					break;
				}
				cont++;

			}

			AdmUser adm = new AdmUser(func[cont]);

		}

	}

	private class Listar implements KeyListener {

		public void keyPressed(KeyEvent e) {

		}

		public void keyReleased(KeyEvent e) {

			TableRowSorter sorter = new TableRowSorter(modelo);

			jTable.setRowSorter(sorter);

			sorter.setRowFilter(RowFilter.regexFilter(".*"
					+ txtBusca.getText().toUpperCase() + ".*"));

			jTable.setModel(modelo);

		}

		public void keyTyped(KeyEvent e) {

		}

	}
	
	
	private class duploClick implements MouseListener{

		@Override
		public void mouseClicked(MouseEvent e) {
			if(e.getClickCount()==2){
				boolean flag = true;
				int cont = 0;

				while (flag) {
					if (jTable.getValueAt(jTable.getSelectedRow(), 0) == dados[cont][0]) {
						flag = false;
						break;
					}
					cont++;

				}

				AdmUser adm = new AdmUser(func[cont]);
			}
			
		}

		@Override
		public void mouseEntered(MouseEvent e) {
			// TODO Auto-generated method stub
			
		}

		@Override
		public void mouseExited(MouseEvent e) {
			// TODO Auto-generated method stub
			
		}

		@Override
		public void mousePressed(MouseEvent e) {
			// TODO Auto-generated method stub
			
		}

		@Override
		public void mouseReleased(MouseEvent e) {
			// TODO Auto-generated method stub
			
		}
		
	}

}
